<?php
/**
 * 组管理
 *@date 2012-1-5
 *@author zhouyt
*/

class Sys_GroupcoreController extends Zend_Controller_Action
{
	private $request;
	private $common;
	private $db;
    public function init()
    {
		$this->request = $this->getRequest();

		include_once "../library/inc/Zyt_Common.php"; 
		$this->common = Zyt_Common::getInstance();
		
		$this->common->checklogin(true, true);

		include_once "../library/inc/Db_Config.php";
		$this->db = Db_Config::getInstance()->Base();

		if(!$this->request->isXmlHttpRequest()) {
			$this->common->PutData('{result:"",error:"非法的请求，请重试或与管理员联系..."}');
		}
        /* Initialize action controller here */
    }

	public function getlistAction()
	{
				$start = $this->request->getParam("start");
		$limit = $this->request->getParam("limit");
			$_iAmount = $this->db->fetchOne("SELECT count(groupid) FROM groupcore WHERE status & 1 = 0");
			$result = $this->db->fetchAll("SELECT g.groupid, g.subject, g.usernumber, g.optionset, g.status, g.remark, g.createon, g.reportday, g.departmentid, d.departname FROM groupcore g INNER JOIN department d on g.departmentid = d.departmentid WHERE g.status & 1 = 0 limit $start, $limit");
			$sMsg = "";
			$len = count($result);
			for($i=0; $i < $len; $i++) {
				$comma = ",";
				if($i == ($len -1)) 
				{
					$comma = "";
				}
				$sMsg .= "{GroupID:" . $result[$i]["groupid"] . ", Subject:'" . $result[$i]["subject"] . "',UsersNumber:" . $result[$i]["usernumber"] . ",Remark:'" . $result[$i]["remark"] . "',OptionSet:" . $result[$i]["optionset"] . ",CreateOn:'" . $result[$i]["createon"] . "',Status:" . $result[$i]["status"] . ",LockStatus: " . $result[$i]["status"] . ",ReportDay:" . $result[$i]["reportday"] . ",DepartmentID:" . $result[$i]["departmentid"] . ",DepartName:'" . $result[$i]["departname"] . "'}" . $comma;
			}

			$acl = array("aRead" => 1, "aNew" => 1, "aEdit" => 1, "aLock" => 1, "aDel" => 1, "aList" => 1);
			$_sExtJSD = "Acl:[{aRead:" . $acl["aRead"] . ", aNew:" . $acl["aNew"] . ",aEdit:" . $acl["aEdit"] . ",aDel:" . $acl["aDel"] .
                                  ",aList:" . $acl["aList"] . "}]";
			$this->common->PutData('{totalProperty:' . $_iAmount . ',records:[' . $sMsg . '],' . $_sExtJSD . '}');
			exit;
		
	}

	public function saveAction()
	{
		$groupid = $this->request->getParam("GroupID");
		$departmentid = $this->request->getParam("DepartmentID");
		$subject = $this->request->getParam("Subject");
		$reportday = $this->request->getParam("ReportDay");
		$reportday = empty($reportday) ? 0 : $reportday;
		$remark = $this->request->getParam("Remark");
		
		if($groupid > 0) {
			if($this->db->update("groupcore", array(
					"departmentid" => $departmentid,
					"subject"  => $subject,
					"reportday" => $reportday,
					"remark"    => $remark
				), $this->db->quoteInto('groupid = ?', $groupid)))
				{
					$this->common->PutData('{result:""}');
				}
				else
				{
					$this->common->PutData('{error:"保存失败!"}');
				}
		}
		else
		{
			if($this->db->insert("groupcore", array(
				"departmentid" => $departmentid,
				"subject"    => $subject,
				"reportday" => $reportday,
				"usernumber" => 0,
				"remark"    => $remark,
				"optionset" => 0,
				"status"	=> 0,
				"createon"  => $this->common->getGmtime()
			))) {
				
				$this->common->PutData('{result:""}');
			}
			else
			{
				$this->common->PutData('{error:"保存失败!"}');
			}
		}
		exit;
	}

	public function delAction()
	{
		$uids = $this->request->getParam("aid");
		
		if($this->db->update("groupcore", array(
					"status" => 1
				), $this->db->quoteInto('groupid in (?)', $uids))) 
		{
			$this->common->PutData('{result:""}');
		}
		else
		{
			$this->common->PutData('{error:"操作失败!"}');
		}
		exit;
	}

	public function lockAction()
	{
		$uids = $this->request->getParam("aid");
		
		$stmt = $this->db->query("UPDATE groupcore SET status = 2 - status & 2 WHERE status in (0,2) AND " . $this->db->quoteInto('groupid in (?)', $uids));
		if($stmt->rowCount())
		{
			$this->common->PutData('{result:""}');
		}
		else
		{
			$this->common->PutData('{error:"操作失败!"}');
		}
		exit;
	}
}
